--
-- Author: shunguo.chen
-- Date: 2019-05-23 20:12:24
--
-- SkillController 技能controller
--

local SkillView = import("..views.SkillView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("SkillModel")

    self._curIdx = 1    -- 当前界面idx
    self._curSpecType = nil -- 当前专精

    self._selectSkillTeam = nil -- SkillPanel 默认选中技能组id

    self._redPointHandler = UD.redPoint:onChanged(handler(self, self.updateRedPoint))
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)

    UD.redPoint:unChanged(self._redPointHandler)
    self._redPointHandler = nil
end

function ClassRef:listEvent()
    return { }
end

-- data = idx
-- 1 SkillPanel
-- 2 TalentPanel
-- 3 RunePanel
function ClassRef:openView(data)
    if nil == self._view then
        self._view = SkillView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self._view:onSwitchPanel(handler(self, self.onSwitchPanel))
        self:addChild(self._view, data)
    end

    self._view:refresh(UD:getSpecTypes(), UD:getCurSpecTypeId())
    self._view:show(data or self._curIdx)
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
    end

    self._curIdx = 1
    self._curSpecType = nil

    UD:clearNewGetRunes()
    UD.redPoint:clearStateByKey(Constant.FUNC_TYPE_SKILL)

    if BattleManager:getNeedReEnterTraining() then
        BattleManager:setNeedReEnterTraining(false)
        BattleManager:reqTraining()
    end
end

-------------------------------------------------------
-- 红点变动(应该在这里精细刷新界面)
function ClassRef:updateRedPoint(changeKeys)
    if not self:isViewAlive() then return end

    if changeKeys[RedPointConstant.Skill_Skill] ~= nil then
        self._view:refreshRedPoint(RedPointConstant.Skill_Skill)
    end

    if changeKeys[RedPointConstant.Skill_Talent] ~= nil then
        self._view:refreshRedPoint(RedPointConstant.Skill_Talent)
    end

    if changeKeys[RedPointConstant.Skill_Rune] ~= nil then
        self._view:refreshRedPoint(RedPointConstant.Skill_Rune)
    end
end

-- 激活专精
function ClassRef:onChooseSpec()
    if not self:isViewVisible() then return end

    self._view:refresh(UD:getSpecTypes(), UD:getCurSpecTypeId())
    self:onSwitchPanel(self._curIdx)
end

-- 技能学习/升级成功
function ClassRef:onSkillUpdate(skill)
    if not self:isViewVisible() then return end
    if self._curIdx ~= 1 then return end

    local panel = self._view:getPanel(self._curIdx)
    panel:updateSkill(skill)
end

-- 选择技能
function ClassRef:onChooseSkill(skillTeam)
    if not self:isViewVisible() then return end
    if self._curIdx ~= 1 then return end

    self._selectSkillTeam = skillTeam
    self:onSwitchPanel(1)
end

-- 天赋更新(激活)
function ClassRef:onTalentUpdate(talent)
    if not self:isViewVisible() then return end
    if self._curIdx ~= 2 then return end

    local panel = self._view:getPanel(self._curIdx)
    panel:updateTalent(talent)
end

-- 天赋重置
function ClassRef:onTalentReset()
    if not self:isViewVisible() then return end
    if self._curIdx ~= 2 then return end

    self:onSwitchPanel(2)
end

-- 镶嵌/卸下符文
function ClassRef:onRuneUpdate(rune)
    if not self:isViewVisible() then return end
    if self._curIdx ~= 3 then return end

    local panel = self._view:getPanel(self._curIdx)
    panel:updateRune(rune)
end

-------------------------------------------------------
function ClassRef:onOperate(op, ...)
    -- print("SkillController:onOperate op", op)
    local data = ...
    if op == "specType_switch" then
        -- 切换专精 data = SpecType
        if not self._curSpecType then
            -- 第一次设置不刷新panel
            self._curSpecType = ...
        else
            self._curSpecType = ...
            self:onSwitchPanel(self._curIdx)
        end
    elseif op == "specType_active" then
        -- 激活专精
        self:openSpecChooseDlg()
    elseif op == "skill_openMore" then
        -- 打开技能切换 data = Skill
        self:openMorePanel(...)
    elseif op == "skill_lvup" then
        -- 技能学习/升级 data = Skill
        self._model:C2S_StudySkill(data:getNextSkillId())
    elseif op == "skill_choose" then
        -- 选择技能 
        local chooseSkill, sourceSkillIdx, isLegend = ...
        local params = {}
        params.teamId = isLegend and chooseSkill:getLegendTeam() or chooseSkill:getTeam()
        params.chooseId = self._curSpecType:getId()
        params.index = sourceSkillIdx - 1
        self._model:C2S_SkillChoose(params)
    elseif op == "talent_active" then
        -- 天赋激活 data = Talent
        self._model:C2S_StudyTalent(data:getId())
    elseif op == "talent_reset" then
        -- 天赋重置
        self:openTalentResetDlg()
    elseif op == "rune_equip" then
        -- 镶嵌符文
        self._model:C2S_PutRune(data:getId())
    elseif op == "rune_unequip" then
        -- 卸下符文
        self._model:C2S_RemoveRune(data:getId())
    end
end

-- 1 SkillPanel
-- 2 TalentPanel
-- 3 RunePanel
function ClassRef:onSwitchPanel(idx)
    self._curIdx = idx
    if not self._curSpecType then return end

    if idx == 1 then
        self:refreshSkillPanel()
    elseif idx == 2 then
        self:refreshTalentPanel()
    elseif idx == 3 then
        self:refreshRunePanel()
    end
end

function ClassRef:refreshSkillPanel()
    if not self:isViewVisible() then return end

    local panel = self._view:getPanel(self._curIdx)
    local skills = UD:getSpecChooseSkills(self._curSpecType)
    panel:refresh(self._curSpecType, skills, self._selectSkillTeam)
    self._selectSkillTeam = nil
end
function ClassRef:refreshTalentPanel()
    if not self:isViewVisible() then return end

    local panel = self._view:getPanel(self._curIdx)
    local talents = UD:getSpecTalents(self._curSpecType:getId())
    panel:refresh(self._curSpecType, talents)
end
function ClassRef:refreshRunePanel()
    if not self:isViewVisible() then return end

    local panel = self._view:getPanel(self._curIdx)
    local runesList = UD:getSpecRunesList(self._curSpecType:getId(), true)
    panel:refresh(self._curSpecType, runesList)
end

function ClassRef:openMorePanel(skill, skillIdx)
    if not self:isViewVisible() then return end
    if self._curIdx ~= 1 then return end

    local panel = self._view:getPanel(self._curIdx)
    local skills = UD:getSpecChangeSkills(self._curSpecType)
    local legend = app:sendMsg("LegendModel", "getWearLegend")
    panel:openMorePanel(skills, skill, skillIdx, legend, self._curSpecType)
end

function ClassRef:openSpecChooseDlg()
    local specId = self._curSpecType:getId()
    local talents = UD:getSpecTalents(specId)
    local isAllActive = true
    for i,talent in ipairs(talents) do
        if not talent:isActive() then
            isAllActive = false
            break
        end
    end

    if isAllActive then
        self._model:C2S_SetSkillChooseID(specId, false)
    else
        local costStr = GD:getSystemStrPar("refreshTalentPointCost", "")
        assert( "" ~= costStr, " ----------->> 天赋重置消耗读取不到 " )
        local costItem = UD:parseRewardToBagItem( costStr )
        local hasNum = UD:getNumByRenderData( costItem ) or -1

        local params = {}
        params.str_1 = L("tips_3000341")
        params.str_2 = L("tips_3000342")
        params.str_3 = L("tips_3000343") .. hasNum
        params.costNum = costItem.amount
        params.costIconPath = costItem:getIcon()
        params.onConfirm = function(sender)
            self._model:C2S_SetSkillChooseID(specId, true)
            sender:removeFromParent()
        end
        params.onCancel = function(sender)
            self._model:C2S_SetSkillChooseID(specId, false)
            sender:removeFromParent()
        end
        local costConfirmDialog = display.newCostConfirmDialog( params )
        UIHandler:addPopDialog( costConfirmDialog )
    end
end

function ClassRef:openTalentResetDlg()
    local specId = self._curSpecType:getId()
    local talents = UD:getSpecTalents(specId)
    local hasActive = false
    for i,talent in ipairs(talents) do
        if talent:isActive() then
            hasActive = true
            break
        end
    end

    if not hasActive then
        display.pushToast(L("当前专精天赋无需重置"))
        return
    end

    local costStr = GD:getSystemStrPar("refreshTalentPointCost", "")
    assert( "" ~= costStr, " ----------->> 天赋重置消耗读取不到 " )
    local costItem = UD:parseRewardToBagItem( costStr )
    local hasNum = UD:getNumByRenderData( costItem )

    local params = {}
    params.str_2 = L("tips_3000342")
    params.str_3 = L("tips_3000343") .. hasNum
    params.costNum = costItem.amount
    params.costIconPath = costItem:getIcon()
    params.onConfirm = function(sender)
        self._model:C2S_ClearTalents()
        sender:removeFromParent()
    end
    params.onCancel = function(sender)
        sender:removeFromParent()
    end
    local costConfirmDialog = display.newCostConfirmDialog( params )
    UIHandler:addPopDialog( costConfirmDialog )
end

return ClassRef
